package com.coocaa.ops.admin.v1.auth.sys.biz.impl;

import com.coocaa.ops.admin.v1.auth.sys.biz.ISysUserProfessionGroupRlsBiz;
import com.coocaa.ops.admin.v1.auth.sys.mapper.ISysUserProfessionGroupRlsMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserProfessionGroupRlsEntity;
import com.coocaa.ops.common.tool.date.DateUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2019/3/14.
 * @description
 */
@Service
public class SysUserProfessionGroupRlsBizImpl extends BaseBizImpl<ISysUserProfessionGroupRlsMapper, SysUserProfessionGroupRlsEntity> implements ISysUserProfessionGroupRlsBiz {

    @Override
    public SysUserProfessionGroupRlsEntity add(SysUserProfessionGroupRlsEntity entity) {
        getMapper().insert(entity);
        return entity;
    }

    /**
     * 保存 业务分类权限信息
     *
     * @param dicts
     * @param userId
     */
    @Override
    public void saveUserProfession(List<Integer> dicts, Integer userId) {
        if (dicts == null || userId == null) {
            return;
        }
        //删除之前的关联信息
        deleteByUserId(userId);
        for (int id : dicts) {
            SysUserProfessionGroupRlsEntity rlsEntity = new SysUserProfessionGroupRlsEntity();
            rlsEntity.setUserId(userId);
            rlsEntity.setProfessionId(id);
            rlsEntity.setCreatedDate(DateUtil.getCurDate());
            add(rlsEntity);
        }
    }

    /**
     * 删除之前的关联信息
     *
     * @param userId
     */
    private void deleteByUserId(Integer userId) {
        QueryTemplate wrapper = QueryTemplate.getEqQueryTemplate("user_id", userId);
        if (getMapper().selectCount(wrapper) > 0) {
            getMapper().delete(wrapper);
        }
    }
}
